package com.powernode.dataservice.service;

import com.powernode.api.model.FinanceAccount;
import com.powernode.api.model.User;
import com.powernode.api.service.UserService;
import com.powernode.dataservice.mapper.FinanceAccountMapper;
import com.powernode.dataservice.mapper.UserMapper;
import com.powernode.util.CommonUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@DubboService(interfaceClass = UserService.class,version = "1.0.0")
public class UserServiceImpl implements UserService {

    @Value("${password.salt}")
    private String passwordSalt;

    /*用户*/
    @Autowired
    private UserMapper userMapper;

    /*账号*/
    @Autowired
    private FinanceAccountMapper financeAccountMapper;

    @Override
    public User queryUserByPhone(String phone) {
        User user = null;
        if (CommonUtil.checkPhone(phone)){
            /*实际开发过程中 不知道 controller是否有验证*/
            user =userMapper.selectUserByPhone(phone);
        }
        return user;
    }

    /**
     * 注册：  二次加密     开立帐户
     * @param phone  手机号码
     * @param loginPassword  登录密码   已经经过一次 md5加密
     * @return
     */
    @Override
    /*有异常就会滚*/
    @Transactional(rollbackFor = Exception.class)
    public synchronized User userRegister(String phone, String loginPassword) {
        User user =null;
        /*1 检查数据*/
        if (CommonUtil.checkPhone(phone)&&loginPassword.length()==32){
            user = new User();
            user.setPhone(phone);
            /*密码 需要  二次加密之前   加盐：拼接一个字符串（）  拼接完后再加密一次*/
            String newPassword = DigestUtils.md5Hex(loginPassword + passwordSalt);
            user.setLoginPassword(newPassword);
            user.setAddTime(new Date());
            /*返回  主键 id   用户  添加 账户表 所需要的 uid*/
            userMapper.insertUserReturnId(user);
            /*开户*/
            FinanceAccount financeAccount = new FinanceAccount();
            /*刚刚 创建好的 用户的 id */
            financeAccount.setUid(user.getId());
            financeAccount.setAvailableMoney(new BigDecimal("0"));
            /*添加账户*/
            financeAccountMapper.insertSelective(financeAccount);
        }
        return user;
    }

    /**
     * 登录
     * @param phone  手机号码
     * @param loginPassword 密码
     * @return
     */
    @Override
    public User userLogin(String phone, String loginPassword) {
        User user =null;
        if (CommonUtil.checkPhone(phone)&&loginPassword!=null&&loginPassword.length()==32){
            String newPasword = DigestUtils.md5Hex(loginPassword + passwordSalt);
            user = userMapper.selectLogin(phone,newPasword);
        }
        return user;
    }

    @Override
    public User queryUserById(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }
}
